package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Banner;
import com.adnaan.backend.service.BannerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * author: adnaan
 * create: 2025-02-19 17:18
 * Description:
 */
@Tag(name = "轮播图管理")
@RestControllerMapping("/banner")
public class BannerController {

    @Resource
    private BannerService bannerService;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "轮播图分页查询")
    public Result<Page<Banner>> pageQuery(@RequestBody PageParams<Banner> pageParams) {
        // 1.查询参数
        Banner params = pageParams.getParams();
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getTitle() != null, Banner::getTitle, params.getTitle());
        // 3.分页查询
        Page<Banner> page = bannerService.page(pageParams.getPage(), lqw);
        return Result.success(page);
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增轮播图数据")
    public Result<Boolean> insert(@RequestBody Banner banner) {
        if (bannerService.save(banner)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新轮播图数据")
    public Result<Boolean> update(@RequestBody Banner banner) {
        if (bannerService.updateById(banner)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除轮播图数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (bannerService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }
}